package com.zzy.netty.learn.demo.future_callback;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.zzy.netty.learn.demo.future_callback.listener.MyHandler;

/**
 * 
 * @author yue
 *
 */
public class FutureCallBackTask implements MyFutureRunnable{

	/**
	 * 1连接  2.可读  3. 可写
	 */
	private volatile int state=1;
	
	
	private Map<String, MyHandler> listenerMap =new ConcurrentHashMap<String, MyHandler>();
	

	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		return false;
	}

	@Override
	public boolean isCancelled() {
		return false;
	}

	@Override
	public boolean isDone() {
		return false;
	}

	@Override
	public Object get() throws InterruptedException, ExecutionException {
		return null;
	}

	@Override
	public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
		return null;
	}

	@Override
	public void run() {
		while(true) {
			if(state==1) {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				listenerMap.get(state+"").onComplete();
				state=2;
			}
			
			if(state==2) {
				try {
					//模拟读取数据
					System.out.println("............正在读数据..............");
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				listenerMap.get(state+"").onComplete();
				state=3;
			}
			
			if(state==3) {
				try {
					//模拟写数据
					System.out.println("............正在写数据..............");
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				listenerMap.get(state+"").onComplete();
				state=2;
			}
			
		}
	}

	@Override
	public void reg(int state, MyHandler handler) {
		listenerMap.put(state+"", handler);
	}

	
	
	

}
